export function isPromise(value) {
  if (value !== null && typeof value === 'object') {
    return value.promise && typeof value.promise.then === 'function';
  }
}

/* export function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
} */

Date.prototype.Format = function (fmt) { //author: meizz 
  var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "h+": this.getHours(), //小时 
    "m+": this.getMinutes(), //分 
    "s+": this.getSeconds(), //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds() //毫秒 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

/**
 * 把规定的json数据换成treejson数据
 * @param json
 * @returns {Array}
 */
export function jsonToTreeJson(json, rootId) {
  let treeJson = [];
  rootId = rootId ? rootId : '1';
  // 先找到第一层后开始递归
  for (let i = 0; i < json.length; i++) {
    if (json[i].parentId == rootId) {
      json[i].key = json[i].id;
      json[i].url = json[i].href;
      findChildrenByPId(json, json[i]);
      treeJson.push(json[i]);
    }
  }
  return treeJson;
};

function findChildrenByPId(json, obj) {
  let children = [];
  for (let i = 0; i < json.length; i++) {
    if (json[i].parentId == obj.id) {
      json[i].key = json[i].id;
      json[i].url = json[i].href;
      json[i].parentName = obj.name;
      findChildrenByPId(json, json[i]);
      children.push(json[i]);
    }
  }
  if (children.length > 0) {
    obj.child = children;
    obj.children = children;
  }
}

// 根据url查询当前三级菜单的选中情况
export let findMenuByUrl = (menus, url) => {
  for (var i = 0; i < menus.length; i++) {
    if (menus[i].href == url) {
      menus[i].parent = findParentByPId(menus, menus[i].parentId);
      return menus[i];
    }
  }
  return null;
}

function findParentByPId(menus, pid) {
  for (var i = 0; i < menus.length; i++) {
    if (menus[i].id == pid) {
      if (menus[i].parentId != 1) {
        menus[i].parent = findParentByPId(menus, menus[i].parentId);
      }
      return menus[i];
    }
  }
  return {};
}

export let setCookie = (c_name, value, expire) => {
  var exdate = new Date()
  exdate.setTime(exdate.getTime() + expire);
  //console.log(exdate.Format("yyyy-MM-dd hh:mm:ss"));
  document.cookie = c_name + "=" + escape(value) +
    ((expire == null) ? "" : ";expires=" + exdate.toGMTString())
}

export let getCookie = (c_name) => {
  if (document.cookie.length > 0) {
    let c_start = document.cookie.indexOf(c_name + "=")
    if (c_start != -1) {
      c_start = c_start + c_name.length + 1
      let c_end = document.cookie.indexOf(";", c_start)
      if (c_end == -1) c_end = document.cookie.length
      return unescape(document.cookie.substring(c_start, c_end))
    }
  }
  return ""
}

//删除cookie
export let delCookie = (name) => {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

/**
 * 生成没有-的UUID
 */
const uuidV1 = require('uuid/v1');
export let uuid = function () {
  return uuidV1().replace(/\-/g, '');
};